import com.mochimedia.ui.*;

/**
* ...
* @author $(DefaultUser)
*/

class com.mochimedia.ui.Cell extends Component {
    
    private var _type:String = "cell";
    
    private var _background:Boolean = true;
    private var _border:Boolean = true;
    private var _round:Number = 0;
    
    private var _bkgd:MovieClip;
    private var _childrenContainer:MovieClip;

    private var _mask:MovieClip;
    
    private var _maskContent:Boolean = false;
    public function get maskContent():Boolean { return _maskContent; }
    public function set maskContent(value:Boolean):Void { 
        _maskContent = value; 
        if (_maskContent) {
            createMask();
        } else {
            _childrenContainer.setMask(null);
        }
    }
    
    public function get width ():Number {
        
        if (_width == 0) return _mc._width;
        else return _width;
        
    }
    
    public function get height ():Number {
        
        if (_height == 0) return _mc._height;
        else return _height;
        
    }
    
    public function get contentWidth ():Number { return Position.getCellContentWidth(this); }
    public function get contentHeight ():Number { return Position.getCellContentHeight(this); }
    
    public function get contentX ():Number { return _childrenContainer._x; }
    public function set contentX (value:Number):Void { _childrenContainer._x = (!isNaN(value)) ? value : _childrenContainer._x; }
    
    public function get contentY ():Number { return _childrenContainer._y; }
    public function set contentY (value:Number):Void { _childrenContainer._y = (!isNaN(value)) ? value : _childrenContainer._y; }
        
    private var _collapse:Boolean = false;
    public function get collapse():Boolean { return _collapse; }
    public function set collapse(value:Boolean):Void { _collapse = value; }
    
    public function get trapMouse ():Boolean { return (_bkgd.onPress != undefined); }
    public function set trapMouse (val:Boolean):Void {
        if (val) {
            if (_background == false) DrawingMethods.rect(_bkgd, true, 0, 0, _width, _height, 0x000000, 0);
            _bkgd.useHandCursor = false;
            _bkgd.onMouseDown = _bkgd.onPress = function ():Void { };
        } else {
            delete _bkgd.onMouseDown;
            delete _bkgd.onPress;
            _bkgd.useHandCursor = true;
        }
    }
    
    private var _childNodes:Array;
    public function get childNodes():Array { return _childNodes; }
    
    private var _sortChildNodes:Boolean = false;
    public function get sortChildNodes():Boolean { return _sortChildNodes; }
    
    public var lastArrangedChildIndex:Number = -1;
    public var lastArrangedChildX:Number = 0;
    public var lastArrangedChildY:Number = 0;
    
    public function get contents ():MovieClip { return _childrenContainer; }
    
    private var _title_tf:TextField;
    
    //
    //
    public function Cell (container:MovieClip, width:Number, height:Number, background:Boolean, border:Boolean, round:Number, position:Position, style:Style) {
        
        init (container, width, height, background, border, round, position, style);
        
        if (_container != null) create();
        
    }
    
    //
    //
    public function init (container:MovieClip, width:Number, height:Number, background:Boolean, border:Boolean, round:Number, position:Position, style:Style):Void {
        
        super.init(container, position, style);
        
        _width = (!isNaN(width)) ? width : _width;
        _height = (!isNaN(height)) ? height : _height;
        
        _background = (background != null) ? background : _style.background;
        _border = (border != undefined) ? border : _style.border;
        
        _round = (round > 0) ? round : 0;

        _childNodes = [];
        
    }
    
    //
    //
    public function create ():Void {
        
        super.create();
        
        if (_parentCell != undefined) {
            if (_width == 0) _width = _parentCell.width - _position.margin_left - _position.margin_right;
            if (_height == 0 && !_position.collapse) _height = _parentCell.height - _position.margin_top - _position.margin_bottom;
            _parentCell.addEventListener(EVENT_BLUR, this, onBlur);
        }
        
        _bkgd = _mc.createEmptyMovieClip("_bkgd", 0);
        
        drawBackground();
        
        _childrenContainer = _mc.createEmptyMovieClip("_children", 1);
        
        if (_maskContent) createMask();
        
        _mc._x = _position.margin_top;
        _mc._y = _position.margin_left;
        
        for (var i:Number = 0; i < _childNodes.length; i++) Component(childNodes[i]).parentCell = this;        
        
    }
    
    //
    //
    private function drawBackground ():Void {
        
        _bkgd.clear();
		
		if (_background && _round > 0) {
			
			Create.background(_bkgd, _width, _height, _style, _border, _round);

		} else {
			
			if (_border && !_background) DrawingMethods.emptyRect(_bkgd, false, 0, 0, _width, _height, _style.borderWidth, _style.borderColor);
			else if (_background) Create.background(_bkgd, _width, _height, _style, _border, _round);
			
		}  
        
    }
    
    //
    //
    private function createMask ():Void {
        
        if (_mask == undefined) _mask = _mc.createEmptyMovieClip("_mask", 2);
        
        _mask.clear();
        
        if (_border) DrawingMethods.rect(_mask, false, _style.borderWidth, _style.borderWidth, _width - _style.borderWidth * 2, _height - _style.borderWidth * 2, _style.backgroundColor);
        else DrawingMethods.rect(_mask, false, 0, 0, _width, _height);

        _mask._visible = false;
        
        _childrenContainer.setMask(_mask);
        
    }
    
    //
    //
    private function redraw ():Void {
        
        if (_collapse || _position.collapse) {
               
            _height = Position.getCellContentHeight(this) + _style.borderWidth * 2;

            if (_maskContent) createMask();
            
            drawBackground();
            
        }
        
    }
	
	//
	//
	public function update ():Void {
		
		_height = Position.getCellContentHeight(this);
		redraw();
		
	}
    
    //
    //
    public function addChild (child:Component):Component {
        
        if (child.created) return null;
        
        for (var i:Number = 0; i < _childNodes.length; i++) if (_childNodes[i] == child) return child;

        _childNodes.push(child);

        child.parentCell = this;
        child.addEventListener(EVENT_FOCUS, this, onChildFocus);
        
        if (child.position.zindex != 1) _sortChildNodes = true;
                
        Position.arrangeContent(this);
        redraw();

        dispatchEvent(EVENT_CHANGE);
        
        return child;

    }

    
    //
    //
    public function removeChild (child:Component):Boolean {
        
        for (var i:Number = _childNodes.length - 1; i >= 0; i--) {
        
            if (_childNodes[i] == child) {
            
                _childNodes.splice(i, 1);
                
                Position.arrangeContent(this);
                
                dispatchEvent(EVENT_CHANGE);
                
                return child.destroy();
                
            }
            
        }
        
        return false;
        
    }
    
    //
    //
    public function clear ():Void {
        
        for (var i:Number = _childNodes.length - 1; i >= 0; i--) removeChild(Component(_childNodes[i]));
        
        lastArrangedChildIndex = -1;
        lastArrangedChildX = lastArrangedChildY = 0;
        
        dispatchEvent(EVENT_CHANGE);
        
    }
    
    //
    //
    public function onChildFocus (e:Event):Void {
        
        for (var i:Number = 0; i < _childNodes.length; i++) {
            
            if (_childNodes[i] != e.target) if (_childNodes[i]["onBlur"] != undefined) _childNodes[i].onBlur();

        }

    }
    
    //
    //
    public function enable ():Void {
        
        super.enable();
        
        for (var i:Number = _childNodes.length - 1; i >= 0; i--) Component(_childNodes[i]).enable();
        
    }
    
    //
    //
    public function disable ():Void {

        super.disable();
        
        for (var i:Number = _childNodes.length - 1; i >= 0; i--) Component(_childNodes[i]).disable();
        
    }
    
    //
    //
    public function hide ():Void {
        
        _mc._visible = false;
        dispatchEvent(EVENT_BLUR);
        for (var i:Number = _childNodes.length - 1; i >= 0; i--) Component(_childNodes[i]).dispatchEvent(EVENT_BLUR);
        
    }
    
    //
    //
    public function onBlur ():Void {
        
        dispatchEvent(EVENT_BLUR);
        
    }
    
    
}